package com.example.demo.controller.back;

import com.example.demo.dao.AccountMapper;
import com.example.demo.pojo.Account;
import com.example.demo.pojo.Page;
import com.example.demo.util.JSONResult;
import com.example.demo.vo.AccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <h3>reqister</h3>
 * <p>后台登陆类</p>
 *
 * @author : chenjunjie
 * @date : 2021-06-21 16:37
 **/
@RestController
@RequestMapping("/back")
@PreAuthorize("hasAnyAuthority('sys_manage')")
public class BackAccountManageController {

    @Autowired
    AccountMapper accountMapper;


    @RequestMapping("/getCurrentAccount")
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public Authentication authentication(Authentication authentication){
        return  authentication;
    }



    @RequestMapping("/getAccountList")
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public List<Object>  findUserList(Page page){
        List<Object> list=new ArrayList<>();
        List<Account> accountList = accountMapper.findAccountList(page,"","");
        page.setTotalCount(accountMapper.getCount());
        list.add(page);
        list.add(accountList);
        return list;
    }

    @RequestMapping("/getAccountListBycondition")
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public List<Object>  accountListBycondition(Page page,String userName,String realName){
        List<Object> list=new ArrayList<>();
        List<Account> accountList = accountMapper.findAccountList(page,userName,realName);
        page.setTotalCount(accountMapper.getCount());
        list.add(page);
        list.add(accountList);
        return list;
    }


    @RequestMapping(value = "/updateAccount",method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public String updateAccount(AccountVo accountVo){
        List<Object> list=new ArrayList<>();
        boolean success = accountMapper.updateAccount(accountVo);
        list.add(accountVo);
        list.add(success);
        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"修改成功",list):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"修改失败",list);
    }

    @RequestMapping(value = "/deleteAccount",method = RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public String deleteAccount(@RequestParam("id") String accountId){
        boolean success = accountMapper.deleteAccount(accountId);
        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"删除成功",null):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"删除失败",null)
                ;

    }

    @RequestMapping("/updateStatus")
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public String updateStatus(@RequestParam("id") String accountId,@RequestParam("status")Integer status){
        boolean success = accountMapper.updateStatus(accountId, status);
        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"状态修改成功",null):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"状态修改失败",null)
                ;

    }
    @RequestMapping("/addAccount")
    @PreAuthorize("hasAnyAuthority('account_manage')")
    public String addAccount(AccountVo accountVo){
        boolean success=false;
        try {
             success = accountMapper.insertAccount(accountVo);
        }catch (DataIntegrityViolationException e){
            //TODO 日志记录
            System.out.println(e.getMessage());
        }
        return success?JSONResult.fillResultString(HttpStatus.OK.value(),"用户添加成功",null):
                JSONResult.fillResultString(HttpStatus.BAD_REQUEST.value(),"用户添加失败",null)
                ;
    }



}
